/** ###################################################################
**     Filename  : SchedulerEvents.C
**     Project   : Motologger_v2
**     Processor : MC9S12XA256CFU
**     Compiler  : CodeWarrior HCS12X C Compiler
**     Date/Time : 12/8/2009, 7:45 PM
**     Contents  :
**         User source code
**
**     (c) Copyright UNIS, a.s. 1997-2008
**     UNIS, a.s.
**     Jundrovska 33
**     624 00 Brno
**     Czech Republic
**     http      : www.processorexpert.com
**     mail      : info@processorexpert.com
** ###################################################################*/

/* MODULE SchedulerEvents */

#include "Scheduler.h"

//data input
#include "Gear.h"
#include "Vbat.h"
#include "TachIn.h"
#include "SpdIn.h"
#include "TachOut.h"
#include "tilt.h"
#include "Accel.h"
#include "Timestamp.h"

//logging output
#include "data.h"
#include "Fonera.h"

#include "Config.h"
extern configFileType Config;


char speedSampleEventId;
char rpmSampleEventId;
char accelSampleEventId;
char vbatSampleEventId;
char gearSampleEventId;
char lsdFlashId;

volatile char grabSpeedFlag=0;
volatile char grabRpmFlag=0;
volatile char grabAccelFlag=0;
volatile char grabVbatFlag=0;
volatile char grabGearFlag=0;

char FonOutBuf[sizeof(gpsDataType)];
vector_t AccelData;


void speedSampleEvent(void){
	grabSpeedFlag=1;
}

/*
extern unsigned long SpdIn_dt;
extern unsigned long SpdIn_idt;
void speedStopCheckEvent(void){
	if(SpdIn_idt>8500000ul)
		SpdIn_dt=0;	
} */

void rpmSampleEvent(void){
	//asynchronous fixed sampling
	grabRpmFlag=1;
}

void accelSampleEvent(void){
	//can't read accel here, interrupt context
	grabAccelFlag=1;
}

void vbatSampleEvent(void){
	grabVbatFlag=1;
}

void gearSampleEvent(void){
	grabGearFlag=1;
}

void lsd0flash(void){
  //LSD0_SetRatio16(0x8000); //set to 50% duty
  //Lsds_NegBit(0);
}

void SchedulerEvents_Disable(){
	Scheduler_DisableEvent(speedSampleEventId);
	Scheduler_DisableEvent(rpmSampleEventId);
	Scheduler_DisableEvent(accelSampleEventId);
	Scheduler_DisableEvent(vbatSampleEventId);
	Scheduler_DisableEvent(gearSampleEventId);
	Scheduler_DisableEvent(lsdFlashId);
}

void SchedulerEvents_Enable(){
  Scheduler_EnableEvent(speedSampleEventId);
	Scheduler_EnableEvent(rpmSampleEventId);
	Scheduler_EnableEvent(accelSampleEventId);
	Scheduler_EnableEvent(vbatSampleEventId);
	Scheduler_EnableEvent(gearSampleEventId);
	Scheduler_EnableEvent(lsdFlashId);

}

void SchedulerEvents_LoadConfig(){

	Scheduler_SetFreq_hz(speedSampleEventId, Config.settings[speedSampling]);
	Scheduler_SetFreq_hz(rpmSampleEventId, Config.settings[rpmSampling]);
	//Scheduler_SetFreq_hz(accelSampleEventId, Config.settings[accelometerSampling]);
	Scheduler_SetFreq_hz(vbatSampleEventId, Config.settings[vBattSampling]);
	Scheduler_SetFreq_hz(gearSampleEventId, Config.settings[gearSampling]);
	switch(Config.settings[ledMode]){
		case alwaysOn:
			//Scheduler_DisableEvent(lsdFlashId);
			//Lsds_ClrBit(0);
			LSD1_SetRatio16(0xFFFF); //100% duty cycle			
			break;
		case slowFlashing:
			LSD1_SetRatio16(0x8000); //50% duty cycle			
			LSD1_SetFreqHz(2);			
			//Scheduler_SetPer_ns(lsdFlashId, 1000000000);
			break;
		case fastFlashing:
			LSD1_SetRatio16(0x8000); //50% duty cycle			
			LSD1_SetFreqHz(10);			
			//Scheduler_SetFreq_hz(lsdFlashId, 20);
			break;
		case gearShift:
			//LSD1_SetRatio16(0x80); //50% duty cycle			
			//LSD1_SetFreqHz(20);			
			//Scheduler_SetFreq_hz(lsdFlashId, 20);
			break;
	}
	
}

void SchedulerEvents_Init(){
	speedSampleEventId   = Scheduler_AddEvent_hz(&speedSampleEvent, Config.settings[speedSampling]);
	rpmSampleEventId		 = Scheduler_AddEvent_hz(&rpmSampleEvent, Config.settings[rpmSampling]);
	//accelSampleEventId	 = Scheduler_AddEvent_hz(&accelSampleEvent, Config.settings[accelometerSampling]);
	vbatSampleEventId	 = Scheduler_AddEvent_hz(&vbatSampleEvent, Config.settings[vBattSampling]);
	gearSampleEventId	 = Scheduler_AddEvent_hz(&gearSampleEvent, Config.settings[gearSampling]);
	lsdFlashId					 = Scheduler_AddEvent_ns(&lsd0flash, 1000000000);
	//Scheduler_AddEvent_ns(&speedStopCheckEvent, 1700000000);

	switch(Config.settings[ledMode]){
		case alwaysOn:
			//Scheduler_DisableEvent(lsdFlashId);
			//Lsds_ClrBit(0);
			LSD1_SetRatio16(0xFFFF); //100% duty cycle			
			break;
		case slowFlashing:
			LSD1_SetRatio16(0x8000); //50% duty cycle			
			LSD1_SetFreqHz(2);			
			//Scheduler_SetPer_ns(lsdFlashId, 1000000000);
			break;
		case fastFlashing:
			LSD1_SetRatio16(0x8000); //50% duty cycle			
			LSD1_SetFreqHz(10);			
			//Scheduler_SetFreq_hz(lsdFlashId, 20);
			break;
		case gearShift:
			//LSD1_SetRatio16(0x80); //50% duty cycle			
			//LSD1_SetFreqHz(20);			
			//Scheduler_SetFreq_hz(lsdFlashId, 20);
			break;
	}
	
}

void SchedulerEvents_RunAllFlagged(){
  
  if(grabSpeedFlag){
  	((velDataType*)FonOutBuf)->tag=vel_tag;
  	((velDataType*)FonOutBuf)->time_stamp=Timestamp_GetVal();
  	((velDataType*)FonOutBuf)->velocity=SpdIn_GetVal();
  	Fonera_Send(FonOutBuf, sizeof(velDataType));
  	grabSpeedFlag=0;
  }
  
  if(grabRpmFlag){
    ((rpmDataType*)FonOutBuf)->tag=rpm_tag;
  	((rpmDataType*)FonOutBuf)->time_stamp=Timestamp_GetVal();
  	((rpmDataType*)FonOutBuf)->RPM=TachIn_GetVal();
  	Fonera_Send(FonOutBuf, sizeof(rpmDataType));
  	grabRpmFlag=0;
  }	
  
  if(grabAccelFlag){
  	//Accel_Update();//cannot be done in an ISR
  	Accel_GetValue(&AccelData);	
	  
	  ((accelDataType*)FonOutBuf)->tag=acc_tag;
  	((accelDataType*)FonOutBuf)->time_stamp=Timestamp_GetVal();
  	((accelDataType*)FonOutBuf)->x=(AccelData.X);
  	((accelDataType*)FonOutBuf)->y=(AccelData.Y); 
  	((accelDataType*)FonOutBuf)->z=(AccelData.Z);  	
  	Fonera_Send(FonOutBuf, sizeof(accelDataType));
  	grabAccelFlag=0;
  }
  
	if(grabVbatFlag){	
	  
	  ((battDataType*)FonOutBuf)->tag=batt_tag;
  	((battDataType*)FonOutBuf)->time_stamp=Timestamp_GetVal();
  	((battDataType*)FonOutBuf)->batt=Vbat_GetVal(); 	
  	Fonera_Send(FonOutBuf, sizeof(battDataType));
	  grabVbatFlag=0;
	}
	
	if(grabGearFlag){
	  ((gearDataType*)FonOutBuf)->tag=gear_tag;
  	((gearDataType*)FonOutBuf)->time_stamp=Timestamp_GetVal();
  	((gearDataType*)FonOutBuf)->gear=Gear_GetVal(); 	
  	Fonera_Send(FonOutBuf, sizeof(gearDataType));
  	grabGearFlag=0;
  }
	
	
}



/* END SchedulerEvents */
